package coml.jiyun.hp.wanandroid.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import coml.jiyun.hp.wanandroid.R;
import coml.jiyun.hp.wanandroid.base.BaseLazyFragment;
import coml.jiyun.hp.wanandroid.bean.BannerBean;
import coml.jiyun.hp.wanandroid.bean.HomeBean;
import coml.jiyun.hp.wanandroid.presenter.HomePresenter;
import coml.jiyun.hp.wanandroid.ui.adapter.HomeAdapter;
import coml.jiyun.hp.wanandroid.view.HomeView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseLazyFragment<HomePresenter>implements HomeView {


    /**
     * 首页
     */
    private TextView mHomeToolTv;
    private Toolbar mHomeTool;
    private ViewPager mHomeVp;
    private TabLayout mHomeTab;
    private ArrayList<Fragment> allfragments;
    private HomeAdapter homeAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mPresenter.getHome();
    }

    @Override
    protected HomePresenter initPresenter() {
        return new HomePresenter();
    }

    public void initView(View inflate) {

        mHomeToolTv = (TextView) inflate.findViewById(R.id.home_tool_tv);
        mHomeTool = (Toolbar) inflate.findViewById(R.id.home_tool);
        mHomeVp = (ViewPager) inflate.findViewById(R.id.home_vp);
        mHomeTab = (TabLayout) inflate.findViewById(R.id.home_Tab);

        mHomeTool.setTitle("");

        allfragments = new ArrayList<>();
        allfragments.add(new HomeTwoFragment());
        allfragments.add(new AnswerFragment());
        allfragments.add(new SystemFragment());
        allfragments.add(new MyFragment());

        homeAdapter = new HomeAdapter(getChildFragmentManager(), allfragments);
        mHomeVp.setAdapter(homeAdapter);
        mHomeTab.setupWithViewPager(mHomeVp);

        mHomeTab.getTabAt(0).setText("首页").setIcon(R.drawable.home);
        mHomeTab.getTabAt(1).setText("问答").setIcon(R.drawable.answer);
        mHomeTab.getTabAt(2).setText("体系").setIcon(R.drawable.system);
        mHomeTab.getTabAt(3).setText("我的").setIcon(R.drawable.my);
        mHomeTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 0) {
                    mHomeToolTv.setText("首页");
                }
                if (position == 1) {
                    mHomeToolTv.setText("问答");
                }
                if (position == 2) {
                    mHomeToolTv.setText("体系");
                }
                if (position == 3) {
                    mHomeToolTv.setText("我的");
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void getHome(HomeBean homeBeane) {

    }

    @Override
    public void getBanner(BannerBean banner) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}